package com.abel;

import com.abel.ignite.impl.RepositoryManager;
import com.abel.ignite.model.AtomType;
import com.abel.ignite.model.RepositoryModel;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collection;
import java.util.List;

/**
 * @author xiachao
 * @date 2019-04-28
 */
@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {

        SpringApplication.run(MainApplication.class, args);

        RepositoryManager manager = ApplicationLocator.getContext().getBean(RepositoryManager.class);
        RepositoryModel model = new RepositoryModel();
        model.setRepository("Animal");
        List<AtomType> list = Lists.newArrayList();
        AtomType t1 = new AtomType();
        t1.setName("name");
        t1.setType("varchar");
        AtomType t2 = new AtomType();
        t2.setName("age");
        t2.setType("int");
        list.add(t1);
        list.add(t2);
        model.setAtomTypes(list);
        //boolean ret = manager.createRepository(model);
        boolean ret = manager.createRepositoryByJDBC(model);
        System.out.println(ret);
        Collection<String> cacheNames = manager.getCacheNames();
        System.out.println(JSON.toJSONString(cacheNames));
        String cacheName = model.getRepository();
        String sql = "insert into Person values(1, 'Jack', 27)";
        ret = manager.insert(cacheName, sql);
        String query = "select * from Person";
        List<?> results =  manager.query(cacheName, query);
        for(int i=0;i<results.size();i++){
            System.out.println(JSON.toJSONString(results.get(i)));
        }
        System.out.println(ret);

        String update = "update Person set name = 'Tom' where id = 1";

        manager.update(cacheName, update);
        results =  manager.query(cacheName, query);
        for(int i=0;i<results.size();i++){
            System.out.println("after : "+JSON.toJSONString(results.get(i)));
        }
    }
}
